﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Kooboo.HtmlFunc.Function;

namespace Kooboo.HtmlFunc.Analysis.Testers
{
    public class StringTester : IFuncTester
    {
        static Regex BlankRegex;

        static StringTester()
        {
            BlankRegex = new Regex(@"\s");
        }

        public static string RemoveBlanks(string str)
        {
            return BlankRegex.Replace(str ?? string.Empty, string.Empty);
        }

        public StringTester(string referenceText)
        {
            this.ReferenceText = RemoveBlanks(referenceText);
            this.IgnoreTextCase = true; // default to true
        }

        public string ReferenceText
        {
            get;
            private set;
        }

        public bool IgnoreTextCase
        {
            get;
            set;
        }

        #region IFuncTester Members

        public virtual bool IsMatch(object real)
        {
            var realString = real as string;

            if (realString == null)
            {
                throw new ArgumentException();
            }

            if (this.IgnoreTextCase)
            {
                return this.ReferenceText.IgnoreCaseEquals(RemoveBlanks(realString));
            }
            else
            {
                return this.ReferenceText == RemoveBlanks(realString);
            }
        }

        #endregion
    }
}
